import {
    parseTimer
} from '../../utils/util'
const $api = require('../../api/index');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sex: '',
        phone_number: "",
        currentDate: null,
        currentDateStr: '',
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },
        columns: [{
                text: '男',
                key: 1
            },
            {
                text: '女',
                key: 2
            }
        ],
        show: false,
        showSex: false,
        head_link: '',
        name: "", //姓名
        alias: "", //微信昵称
        birthday: "",
        car_license:"",
        car:""
    },
    inputChange: function (e) {
        this.setData({
            "car": e.detail.value
        })
    },

    setBirthday() {
        this.setData({
            show: true,
        });
    },
    setSex() {
        this.setData({
            showSex: true,
        })
    },
    onClose() {
        this.setData({
            show: false,
            showSex: false
        })
    },
    confirm(event) {
        this.setData({
            currentDate: event.detail,
            birthday: parseTimer(event.detail),
            show: false
        });
    },
    confirmSex(event) {
        this.setData({
            sex: event.detail.value.key,
            showSex: false
        });
    },
    setphone() {
        wx.navigateTo({
            url: `../editPhone/index`,
        })
    },
    addCar() {
        wx.navigateTo({
            url: `../addCar/addCar?car_license=${this.data.car_license?this.data.car_license:''}`,
        })
    },
    submit() {
        const prams = {};
        prams.name = this.data.name;
        prams.birthday = this.data.birthday ? new Date(this.data.birthday).getTime() : "";
        prams.sex = this.data.sex;
        prams.head_link = this.data.head_link;
        prams.phone_number = this.data.phone_number;
        prams.car_license = this.data.car_license;
        prams.car = this.data.car;
        
        $api.userEdit(prams).then((res) => {
           if(res.result==1){
               wx.showToast({
                 title: '修改成功',
                 icon:'none',
                 duration: 3000
               });
               wx.navigateBack();
           }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
       this.getData();
    },
    getData() {
        $api.userCenter().then((res) => {
            if (res.data) {
                this.setData({
                    alias: res.data.alias,
                    head_link: res.data.head_link,
                    phone_number: res.data.phone_number,
                    birthday: res.data.birthday ? parseTimer(new Date(res.data.birthday).getTime()) : "",
                    name: res.data.name,
                    sex: res.data.sex,
                    user_code: res.data.user_code,
                    car_license: res.data.car_license,
                    car: res.data.car_brand_name
                });
            }
        });
    },
    bindKeyInput: function (e) {
        this.setData({
          name: e.detail.value
        })
      },
    onChooseAvatar(e) {
        const { avatarUrl } = e.detail 
        this.setData({
            head_link:avatarUrl,
        })
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
    
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow(options) {
        let pages = getCurrentPages();
        // 数组中索引最大的页面--当前页面
        let currentPage = pages[pages.length - 1];
        // 打印出当前页面中的 options
        this.setData({
            car_license:currentPage.options.car_license  
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

})